//
//  HEIMProductCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2021/1/20.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HEIMProductCell.h"
#import "HEIMProductObject.h"

@interface HEIMProductCell ()

@property(nonatomic, strong) UILabel *nameLb;
@property(nonatomic, strong) UILabel *priceLb;
@property(nonatomic, strong) UIImageView *iconView;

@end

@implementation HEIMProductCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
  
        [self initUI];
        [self makeGesture];
    }
    return self;
}

- (void)makeGesture {
    [super makeGesture];
    UITapGestureRecognizer *contentTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(contentClick)];
    [self.bubbleView addGestureRecognizer: contentTap];
}

- (void)contentClick {
    if ([self.delegate respondsToSelector:@selector(onTapMessage:)]) {
        [self.delegate onTapMessage: self.model.message];
    }
}

- (void)initUI {
    
    UIView *warp = [UIView new];
    warp.backgroundColor = KWhiteColor;
    warp.layer.cornerRadius = 10;
    warp.layer.masksToBounds = YES;
    [self.bubbleView addSubview:warp];

    [warp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bubbleView);
        make.left.equalTo(self.bubbleView);
        make.right.equalTo(self.bubbleView);
        make.height.mas_greaterThanOrEqualTo(70 + 20);
        make.bottom.equalTo(self.bubbleView);
    }];
    
    UILabel *noticeLb = [UILabel new];
    noticeLb.text = @"向您推荐服务：";
    noticeLb.font = DYBlodFont(18);
    noticeLb.numberOfLines = 0;
    [warp addSubview: noticeLb];
     
    _iconView = [UIImageView new];
    _iconView.contentMode = UIViewContentModeScaleAspectFill;
    [warp addSubview: _iconView];

    _nameLb = [UILabel new];
    _nameLb.text = @"-";
    _nameLb.font = DYFont(16);
    _nameLb.numberOfLines = 0;
    [warp addSubview: _nameLb];
    
    _priceLb = [UILabel new];
    _priceLb.text = @"-";
    _priceLb.font = DYBlodFont(16);
    _priceLb.textColor = CPriceColor;
    [warp addSubview: _priceLb];
    
    [noticeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(warp).mas_offset(10);
        make.left.equalTo(warp).mas_offset(10);
        make.right.equalTo(warp).mas_offset(-10);
    }];
 
    [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(noticeLb.mas_bottom).mas_offset(8);
        make.left.equalTo(warp).mas_offset(10);
        make.right.equalTo(warp).mas_offset(-10);
        make.height.mas_offset(90);
    }];
    _iconView.layer.masksToBounds = YES;
    _iconView.layer.cornerRadius = 8;
     
    [_nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_iconView.mas_bottom).mas_offset(8);
        make.left.equalTo(warp).mas_offset(10);
        make.right.equalTo(warp).mas_offset(-10);
    }];
    
    [_priceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_nameLb.mas_bottom).mas_offset(10);
        make.left.equalTo(warp).mas_offset(10);
        make.right.equalTo(warp).mas_offset(-10);
        make.bottom.equalTo(warp).mas_offset(-10);
    }];
}

- (void)configCellWithModel:(HEIMMessageModel *)model {
    
    HEIMProductObject *product = (HEIMProductObject *)model.message.messageObject;

    _nameLb.text = product.product_name;
    _priceLb.text = [NSString stringWithFormat:@"￥%@", product.product_price];
    [_iconView sd_setImageWithURL:[NSURL URLWithString:product.product_icon] placeholderImage:IMAGE_NAMED(@"noImage")];
    
    [_nameLb mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(model.contentWidth);
    }];
    
    // 必须写
    [self refreshData:model];
}

@end
